package bandidong.bean;

import bandidong.dao.CustomerDAO;
import bandidong.dao.impl.CustomerDAOImpl;
import bandidong.model.Customer;
import bandidong.transfermodel.SearchCustomerTransferModel;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.ejb.TransactionManagement;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
@TransactionManagement(javax.ejb.TransactionManagementType.CONTAINER)
public class CustomerBean implements CustomerBeanRemote {
    
    @PersistenceContext(unitName = "persistenceUnit")
    EntityManager em;
    
    private CustomerDAO customerDAO = null;
    
    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Customer> getAllCustomers() {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.getAllCustomers();
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Customer getCustomerById(int customerId) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.getCustomerById(customerId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Customer addCustomer(Customer customer) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.addCustomer(customer);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String updateCustomer(Customer customer) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.updateCustomer(customer);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public String deleteCustomer(Customer customer) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.deleteCustomer(customer);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Customer> getCustomersCriteria(SearchCustomerTransferModel model, int page, int pageSize) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.getCustomersCriteria(model, page, pageSize);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public List<Customer> getCustomersCriteria(SearchCustomerTransferModel model) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.getCustomersCriteria(model);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkEmailExists(String customerEmail) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.checkEmailExists(customerEmail);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkAccountNumberExists(String customerAccountNumber) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.checkAccountNumberExists(customerAccountNumber);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkEmailExists(String customerEmail, int customerId) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.checkEmailExists(customerEmail, customerId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean checkAccountNumberExists(String customerAccountNumber, int customerId) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.checkAccountNumberExists(customerAccountNumber, customerId);
    }

    @Override
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public Customer checkCustomerExists(String customerEmail, String customerPassword) {
        customerDAO = new CustomerDAOImpl(em);
        return customerDAO.checkCustomerExists(customerEmail, customerPassword);
    }
}
